<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加新员工</title>
<link rel="stylesheet" href="../public/layui/css/layui.css" media="all">
<script src="../public/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div>
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
		<legend><h2>新员工信息</h2></legend>
	</fieldset>
	<form action="" method="post" class="layui-form">
		<div class="layui-form-item">
		    <label class="layui-form-label">登录名：</label>
		    <div class="layui-input-block">
		    	<input type="text" name="userid" lay-verify="required" lay-reqtext="不能为空！" placeholder="用于登录系统，建议以手机号作为登录名" autocomplete="off" class="layui-input" style="width:500px;">
		   	</div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">登录密码：</label>
	        <div class="layui-input-block">
	            <input type="text" name="pwd" lay-verify="required" lay-reqtext="为了安全，还是给个密码吧" placeholder="登录密码" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">员工真实姓名：</label>
	        <div class="layui-input-block">
	            <input type="text" name="name" lay-verify="required" lay-reqtext="不能为空！" placeholder="尊姓大名" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">性别：</label>
	        <div class="layui-input-block">
		    	<input type="radio" name="sex" value="禁" title="暂不选择" checked="">
	            <input type="radio" name="sex" value="男" title="男">
			    <input type="radio" name="sex" value="女" title="女">
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">年龄：</label>
	        <div class="layui-input-block">
	            <input type="text" name="age" placeholder="选填项" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">联系电话：</label>
	        <div class="layui-input-block">
	            <input type="text" name="phone" lay-verify="required" lay-reqtext="不能为空！" placeholder="请填常用号码" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">账户状态：</label>
	        <div class="layui-input-block">
	            <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchTest" lay-text="正常|禁用">
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">任职的部门与职位：</label>
	        <div class="layui-input-inline">
			    <select name="deptid" id="deptid" lay-filter="deptrole">
			    	<option value="">请选择部门</option>
		        </select>
		    </div>
		    <div class="layui-input-inline">
			    <select name="roleid" id="roleid" lay-filter="deptrole">
			        <option value="">请选择职位</option>
			    </select>
		    </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">基本薪资：</label>
	        <div class="layui-input-block">
	            <input type="text" name="basewages" id="basewages" lay-verify="required" lay-reqtext="不能为空！" placeholder="底薪" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">加班薪资标准：</label>
	        <div class="layui-input-block">
	            <input type="text" name="overtimestandard" id="overtimestandard" lay-verify="required" lay-reqtext="不能为空！" placeholder="一般加班标准，按小时算" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">请假扣薪标准：</label>
	        <div class="layui-input-block">
	            <input type="text" name="leavestandard" id="leavestandard" lay-verify="required" lay-reqtext="不能为空！" placeholder="按次数算" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">旷工扣薪标准：</label>
	        <div class="layui-input-block">
	            <input type="text" name="absentstandard" id="absentstandard" lay-verify="required" lay-reqtext="不能为空！" placeholder="按次数算" class="layui-input" style="width:500px;" />
	        </div>
	    </div>
	    <div class="layui-form-item">
	        <div class="layui-input-block">
	      		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
	            <button type="submit" name="submit" lay-submit="" lay-filter="submit1" class="layui-btn" >确认添加</button>
	        </div>
	    </div>
	</form>
<script type="text/javascript">
	//动态构建部门下拉列表
	function selectDept(){
	//普通html构建
//		var dept = document.getElementById("deptid");
// 		var option = document.createElement("OPTION");
// 		option.value="11";
// 		option.text="第一个";
// 		dept.options.add(option);
// 		console.log(dept);

//layui构建表单下拉列表
		//部门下拉列表
		$.ajax({
			url:'../HumanServlet?button=4&getType=dept'
			,type:'post'
			,async:false
			,success:function(res){
				res = eval('('+res+')');
				var data = res.data;
				for(i in data){
					$("#deptid").append("<option value=\""+data[i].itemId+"\">"+data[i].itemName+"</option>");	//构建下拉列表
				}
// 				layui.form.render("select");	//必须重新渲染
// 				var selectItem = document.getElementById("deptid");
// 				console.log(selectItem);
			}
		});
		//职位下拉列表
		$.ajax({
			url:'../HumanServlet?button=4&getType=role'
			,type:'post'
			,async:false
			,success:function(res){
				res = eval('('+res+')');
				var data = res.data;
				for(i in data){
					$("#roleid").append("<option value=\""+data[i].itemId+"\">"+data[i].itemName+"</option>");	//构建下拉列表
				}
// 				var selectItem = document.getElementById("roleid");
// 				console.log(selectItem);
			}
		})
		layui.form.render("select");	//一起重新渲染
	};
layui.use(['form','jquery'],function(){
	$ = layui.jquery;
	var form = layui.form;
	var layer = layui.layer;
	selectDept();	//页面加载时就自动执行
	
	form.on('submit(submit1)',function(data){
		if(data.field.deptid.length !=0 && data.field.roleid.length != 0){
			
			console.log(data);
			var userid = data.field.userid;
			var deptid = data.field.deptid;
			var roleid = data.field.roleid;
			var pwd = data.field.pwd;
			var status = data.field.status=='on'?1:0;
			var name = data.field.name;
			var sex = data.field.sex;
			var age = data.field.age;
			var phone = data.field.phone;
			var basewages = data.field.basewages;
			var overtimestandard = data.field.overtimestandard;
			var leavestandard = data.field.leavestandard;
			var absentstandard = data.field.absentstandard;
// 			return false;
			$.ajax({
				url:'../HumanServlet?button=7&userid='+userid+'&deptid='+deptid+'&roleid='+roleid+'&pwd='+pwd
											+'&status='+status+'&name='+name+'&sex='+sex+'&age='+age+'&phone='+phone
											+'&basewages='+basewages+'&overtimestandard='+overtimestandard
											+'&leavestandard='+leavestandard+'&absentstandard='+absentstandard
				,type:'post'
				,async:false
				,success:function(res){
					parent.layer.msg(res);
				}
				,error:function(res){
					parent.layer.msg("系统异常");
				}
			});
			//控制提交成功后窗口自动关闭 
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
		}else{
			layer.msg("部门或职位填写不完整！");
			return false;
		}
	});
	form.on('select(deptrole)',function(obj){
		console.log(obj);
		layer.msg("111");
		var deptid = document.getElementById("deptid").value;
		var roleid = document.getElementById("roleid").value;
		if(deptid.length != 0 && roleid.length != 0){
			$.ajax({
				url:'../HumanServlet?button=6&deptid='+deptid+'&roleid='+roleid
				,type:'post'
				,async:false
				,success:function(res){
					res = eval('('+res+')');
					var data = res.data;
				//	console.log(res);
				//	console.log(data);
					document.getElementById("basewages").value=data.basewages;
					document.getElementById("overtimestandard").value=data.overtimestandard;
					document.getElementById("leavestandard").value=data.leavestandard;
					document.getElementById("absentstandard").value=data.absentstandard;
				}
			});
		}else{
			document.getElementById("basewages").value="";
			document.getElementById("overtimestandard").value="";
			document.getElementById("leavestandard").value="";
			document.getElementById("absentstandard").value="";
		}
	});
})
</script>
</div>
</body>
</html>